package leetcode;

import 排序查找.bubble;
import 排序查找.common;

public class 三数最大乘积 {
    // 暴力法 先排序 在求结果
    public  static int violent_test(int[] nums) {
        bubble.bubble_test(nums);
        int a = nums[0]*nums[1]*nums[nums.length-1],b=nums[nums.length-1]*nums[nums.length-2]*nums[nums.length-3];
        if(a>b) {
            return a;
        }
        else {
            return b;
        }
    }
    // 线性扫描
    public static int queue_scan(int[] nums) {
        int min1 = 1000,min2 = 1000;
        int max1 = -1000,max2 = -1000,max3 = -1000;
        for (int i = 0; i < nums.length; i++) {
            if (nums[i]<min1) {
                min2=min1;
                min1 = nums[i];
            }
            else if (nums[i]<min2) {
                min2 = nums[i];
            }
        }
        for (int i = 0; i < nums.length; i++) {
            if (nums[i]>max1) {
                max3 = max2;
                max2 = max1;
                max1 = nums[i];
            }
            else if (nums[i]>max2) {
                max2 = max1;
                max1 = nums[i];
            }
            else if (nums[i]>max3) {
                max1 = nums[i];
            }
        }
        if (max1*max2*max3>=max1*min1*min2) {
            return max1*max2*max3;
        }
        else {
            return max1*min1*min2;
        }
    }

    public static void main(String[] args) {

        System.out.println(violent_test(new int[]{-3,-6,1,4,5,-5}));
        System.out.println(queue_scan(new int[]{-3,-6,1,4,5,-5}));
    }
}
